package ym.controller.meal;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import ym.pojo.Meal;
import ym.service.MealService;
import ym.vo.BaseResult;

import java.util.List;

/**
 * 后台餐食
 */
@CrossOrigin
@RestController
@RequestMapping("/back/meal")
public class MealCon {
    @DubboReference
    private MealService mealService;

    /**
     * 新增
     * z
     *
     * @param meal
     * @return 执行结果
     */
    @PostMapping("/add")
    @PreAuthorize("hasAnyAuthority('/meal')")//springsecurity鉴权注解
    public BaseResult add(Meal meal) {
        mealService.add(meal);
        return BaseResult.ok();
    }


    /**
     * 修改
     *
     * @param meal
     * @return 执行结果
     */
    @PostMapping("/update")
    public BaseResult update(Meal meal) {
        mealService.update(meal);
        return BaseResult.ok();
    }


    /**
     * 删除
     *
     * @param mealId
     * @return 执行结果
     */
    @DeleteMapping("/delete")
    public BaseResult delete(Integer mealId) {
        mealService.delete(mealId);
        return BaseResult.ok();
    }


    /**
     * 根据id查询
     *
     * @param mealId
     * @return 查询结果
     */
    @GetMapping("/findById")
    public BaseResult findById(Integer mealId) {
        Meal meal = mealService.findById(mealId);
        return BaseResult.ok(meal);
    }

    /**
     * 查询所有
     *
     * @return
     */
    @GetMapping("/findAll")
    public BaseResult findAll() {
        List<Meal> all = mealService.findAll();
        return BaseResult.ok(all);
    }

    /**
     * 分页查询
     *
     * @param search 搜索条件
     * @param page   当前页
     * @param size   每页大小
     * @return
     */
    @GetMapping("/search")
    @PreAuthorize("hasAnyAuthority('/meal')")
    public BaseResult search(String search, int page, int size) {
        Page<Meal> mealPage = mealService.search(search, page, size);
        return BaseResult.ok(mealPage);
    }
}
